/******************************************************************************
 * Copyright (C) 2011 Shenzhen Comtop Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳康拓普开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.code.common.mongodb;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * MongoDBQuery工具类
 *
 * @author 陈大周
 * @since jdk 1.5
 * @version 2011-04-21 陈大周 新建
 */
public class MongoDBQueryUtils {

    /**
     * AND查询
     */
    private final BasicDBObject queryAllConditon = new BasicDBObject();

    /**
     * OR查询
     */
    private final BasicDBObject queryOrConditon = new BasicDBObject();

    /**
     * 排序条件
     */
    private final BasicDBObject order = new BasicDBObject();

    /**
     * 更新条件
     */
    private final BasicDBObject objUpdateCondition = new BasicDBObject();

    /**
     * 每页的记录数
     */
    private int pageSize = 1;

    /**
     * 页数
     */
    private int pageNo = 1;

    /**
     * 返回查询条件
     *
     * @return 查找对象
     */
    public BasicDBObject getQueryConditon() {
        if (queryOrConditon.size() > 0) {
            queryAllConditon.put("$or", queryOrConditon);
        }
        return queryAllConditon.size() > 0 ? queryAllConditon : null;
    }

    /**
     * 获取查询条件
     *
     * @return 查询条件
     */
    public BasicDBObject getUpdateConditon() {
        return objUpdateCondition.size() > 0 ? objUpdateCondition : null;
    }

    /**
     * 返回排序
     *
     * @return 排序字段
     */
    public BasicDBObject getOrder() {
        return order.size() > 0 ? order : null;
    }

    /**
     * 添加查询条件
     *
     * @param parameter 参数
     * @param operate 操作
     * @param value 实体值
     */
    public void addAndQuery(String parameter, String operate, Object value) {
        if ("=".equals(operate)) {
            queryAllConditon.put(parameter, value);
        } else {
            queryAllConditon.put(parameter, transQuery(parameter, operate, value));
        }
    }

    /**
     * 添加查询条件
     *
     * @param parameter 参数
     * @param operate 操作
     * @param value 参数值
     */
    public void addArrayQuery(String parameter, String operate, Object value) {
        if ("=".equals(operate)) {
            queryAllConditon.put(parameter, value);
        } else {
            queryAllConditon.put(parameter, transQuery(parameter, operate, value));
        }
    }

    /**
     * 添加查询条件
     *
     * @param parameter 参数
     * @param value 参数值
     */
    public void addOrder(String parameter, String value) {
        order.put(parameter, "ASC".equals(value.toUpperCase()) ? 1 : -1);
    }

    /**
     * 转化查询条件
     *
     * @param parameter 参数
     * @param operate 操作类型
     * @param value 参数值
     * @return 查询条件
     */
    private BasicDBObject transQuery(String parameter, String operate, Object value) {
        BasicDBObject objResult = null;
        if (">".equals(operate)) {
            objResult = new BasicDBObject("$gt", value);
        } else if ("<".equals(operate)) {
            objResult = new BasicDBObject("$lt", value);
        } else if (">=".equals(operate)) {
            objResult = new BasicDBObject("$gte", value);
        } else if ("<=".equals(operate)) {
            objResult = new BasicDBObject("$lte", value);
        } else if ("!=".equals(operate)) {
            objResult = new BasicDBObject("$ne", value);
        } else if ("nin".equals(operate)) {
            objResult = new BasicDBObject("$nin", value);
        } else if ("in".equals(operate)) {
            objResult = new BasicDBObject("$in", value);
        } else if ("size".equals(operate)) {
            objResult = new BasicDBObject("$size", value);
        } else if ("exists".equals(operate)) {
            objResult = new BasicDBObject("$exists", value);
        }
        return objResult;
    }

    /**
     * 更新条件
     *
     * @param parameter 参数
     * @param operate 操作
     * @param value 参数值
     */
    public void updateCondition(String parameter, String operate, Object value) {
        BasicDBObject objResult = null;
        if ("set".equals(operate)) {
            objResult = new BasicDBObject(parameter, value);
        }
        objUpdateCondition.put("$set", objResult);
    }

    /**
     * 添加查询条件
     *
     * @param parameter 参数
     * @param operate1 操作
     * @param value1 参数值
     * @param operate2 操作
     * @param value2 参数值
     */
    public void addArrayQuery(String parameter, String operate1, String value1, String operate2, String value2) {
        DBObject objObject = new BasicDBObject(transOperate(operate1), value1).append(transOperate(operate2), value2);
        queryAllConditon.put(parameter, objObject);
    }

    /**
     * 操作类型转换
     *
     * @param operate 操作
     * @return 返回转化好的字符串
     */
    public String transOperate(String operate) {
        if (">".equals(operate)) {
            operate = operate.replace(operate, "$gt");
        } else if ("<".equals(operate)) {
            operate = operate.replace(operate, "$lt");
        } else if (">=".equals(operate)) {
            operate = operate.replace(operate, "$gte");
        } else if ("<=".equals(operate)) {
            operate = operate.replace(operate, "$lte");
        } else if ("!=".equals(operate)) {
            operate = operate.replace(operate, "$ne");
        } else if ("nin".equals(operate)) {
            operate = operate.replace(operate, "$nin");
        } else if ("in".equals(operate)) {
            operate = operate.replace(operate, "$in");
        } else if ("size".equals(operate)) {
            operate = operate.replace(operate, "$size");
        } else if ("exists ".equals(operate)) {
            operate = operate.replace(operate, "$exists");
        }
        return operate;
    }

    /**
     * 获取 pageNo属性值
     *
     * @return 属性值
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * 获取 pageSize属性值
     *
     * @return 属性值
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 设置pageNo属性值
     *
     * @param pageNo 属性值
     */
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    /**
     * 设置pageSize属性值
     *
     * @param pageSize 属性值
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
}